package se.wobbia.android.blog;

import se.wobbia.android.data.User;
import se.wobbia.android.service.LoginService;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginPage extends Activity implements OnClickListener {
	public static final int MENU_ITEM_NETWORKSETTINGS= Menu.FIRST;
	public static final int MENU_ITEM_ACCOUTSETTINGS = Menu.FIRST;
	private static final String TEXT_USERNAME_KEY = "USERNAME";
	private static final String TEXT_PASSWORD_KEY = "PASSWORD";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpage);
		Button login = (Button) super.findViewById(R.id.loginButton);
		login.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		LoginService loginService = new LoginService();

		EditText userNameEditText = ((EditText) findViewById(R.id.user_name));
		EditText passwordEditText = ((EditText) findViewById(R.id.password));
		String userName = userNameEditText.getText().toString();
		String password = passwordEditText.getText().toString();

		User user = new User(userName, password);
		if (loginService.login(user)) {
			Intent intent = new Intent(LoginPage.this, MainPage.class);
			Log.i("LOGIN", "login successfully");
			startActivity(intent);
		}
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // This is our one standard application action -- inserting a
        // new note into the list.
        menu.add(0, MENU_ITEM_NETWORKSETTINGS, 0, R.string.network_settings)
                .setShortcut('3', 'a')
                .setIcon(android.R.drawable.ic_menu_search);
        menu.add(0, MENU_ITEM_ACCOUTSETTINGS, 0, R.string.account_settings)
        .setShortcut('3', 'a')
        .setIcon(android.R.drawable.ic_menu_add);

        return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_ITEM_NETWORKSETTINGS:
            // Launch activity to insert a new item
            startActivity(new Intent(this,SettingPage.class));
            return true;
        }
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// Get the activity preferences object.
		SharedPreferences uiState = getPreferences(0);
		// Get the preferences editor.
		SharedPreferences.Editor editor = uiState.edit();
		EditText userNameEditText = ((EditText) findViewById(R.id.user_name));
		EditText passwordEditText = ((EditText) findViewById(R.id.password));
		String userName = userNameEditText.getText().toString();
		String password = passwordEditText.getText().toString();
		
		// Add the UI state preference values.
		editor.putString(TEXT_USERNAME_KEY, userName);
		editor.putString(TEXT_PASSWORD_KEY, password);
		// Commit the preferences.
		editor.commit();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// Get the activity preferences object.
		SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
		String userName = settings.getString(TEXT_USERNAME_KEY, "");
		String password = settings.getString(TEXT_PASSWORD_KEY, "");
		EditText userNameEditText = ((EditText) findViewById(R.id.user_name));
		EditText passwordEditText = ((EditText) findViewById(R.id.password));
		userNameEditText.setText(userName);
		passwordEditText.setText(password);
	}

}
